<template>
  <div class="address">
    <van-nav-bar title="地址管理" left-text="返回" left-arrow @click-left="onClickLeft" fixed />
    <div class="content">
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
      />
    </div>
  </div>
</template>
<script>
export default {
  mounted: function() {
    this.getData();
  },
  data() {
    return {
      chosenAddressId: "1",
      pageNum: "",
      pageSize: "",
      list: [],
      contacts: []
    };
  },
  methods: {
    onAdd() {
      this.$router.push("/add");
    },

    getData() {
      function convertKey(arr, key) {
        let newArr = [];
        arr.forEach((item, index) => {
          let newObj = {};
          for (var i = 0; i < key.length; i++) {
            newObj[key[i]] = item[Object.keys(item)[i]];
          }
          newArr.push(newObj);
        });

        return newArr;
      }
      let paramForm = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      this.$axios.get("prod-api/app/address/list", paramForm).then(res => {
        if (200 == res.data.code) {
          var shuju = res.data.rows;
          for (var i = 0; i < shuju.length; i++) {
            var item = shuju[i];
            if (item.isDefault == 0) {
              item.isDefault = 1;
            } else if (item.isDefault == 1) {
              item.isDefault = 0;
            }
          }
          this.list = shuju;
          this.list = convertKey(res.data.rows, [
            "searchValue",
            "createBy",
            "createTime",
            "updateBy",
            "updateTime",
            "version",
            "delFlag",
            "remark",
            "dataScope",
            "params",
            "id",
            "userId",
            "name",
            "tel",
            "province",
            "provinceName",
            "",
            "",
            "",
            "",
            "address",
            "isDefault"
          ]);

          console.log(this.list);
        } else {
        }
      });
    },
    onEdit(item, index) {
      this.$router.push({
        name: "edit",
        params: {
          id: item.id,
          name: item.name,
          tel: item.tel,
          address: item.address,
          isDefault: item.isDefault
        }
      });
    },
    onClickLeft() {
      this.$router.go(-1);
    }
  }
};
</script>
<style lang="scss" scoped>
.content {
  padding-top: 50px;
}
.van-button {
  background-color: dodgerblue;
  border: 0;
}
.van-nav-bar {
  background: url("../assets/bg2.png");
}
</style>